import axios from 'axios';

const service = axios.create({
  withCredentials: true,
  timeout: 50000
})

const isDevelopment = import.meta.env.DEV;
const baseURL = localStorage.getItem('baseUrl') || '';
const request_prefix = import.meta.env.VITE_REQUEST_PREFIX;
const token = localStorage.getItem("Token") || import.meta.env.VITE_APPLICATION_TOKEN;

// 请求拦截
service.interceptors.request.use(
  (config) => {
    config.headers['Authorization'] = 'Bearer ' + token;
    config.baseURL = isDevelopment ? "/api-dev" : baseURL + "/factory/v1";
    if(isDevelopment){
      config.headers['x-request-wan-access'] = true;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
)

// 响应拦截
service.interceptors.response.use(
  (response) => {
    return response;
  },
  (error) => {
    return Promise.reject(error)
  }
)

export default service;
